﻿using DomainService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PcProject.Application.Service.Bargins;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MiniPc.API.Controllers
{
    /// <summary>
    /// 砍价控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize] //授权过滤
    public class BargainController : ControllerBase
    {
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        private IBarginService _barginService;
        private IProductBargain _productBargain;
        /// <summary>
        /// 函数依赖注入
        /// </summary>
        /// <param name="barginService"></param>
        /// <param name="productBargain"></param>
        public BargainController(IBarginService barginService, IProductBargain productBargain)
        {
            _barginService = barginService;
            _productBargain = productBargain;
        }


        /// <summary>
        /// 砍价显示查询分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            try
            {
                List<BargainDto> lst = _barginService.GetBargainAsync();
                List<BargainDto> list= lst.Where(x => x.StartTime <= DateTime.Now && x.EndTime >= DateTime.Now).ToList();
                return Ok(new { data = list, code = 0, msg = "" });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 添加砍价
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddBargain([FromForm] Bargain obj)
        {
            try
            {
                int n = await _barginService.InserAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = obj, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除砍价
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelBargain(int id)
        {
            try
            {
                int n = await _barginService.DeleAsync(id);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 反填砍价
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Fill(int id)
        {
            try
            {
                Bargain n = await _barginService.FillAsync(id);
                return Ok(new { data = n, code = 1 });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 反填砍价
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Fills(int id)
        {
            try
            {
                BargainDto n = _barginService.Fill(id);
                return Ok(new { data = n, code = 1 });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 修改砍价
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UptBargain([FromForm] Bargain obj)
        {
            try
            {
                int n = await _barginService.UptAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 随机砍价
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Algorithm(int id)
        {
            int n = _productBargain.ProBargain(id);
            if (n > 0)
            {
                return Ok(new { data = n, code = 1 });
            }
            else
            {
                return Ok(new { data = n, code = 0 });
            }
        }

    }
}
